@rem set VSCMD_DEBUG=2

call :DetectVsPath     VisualStudioCmd
call :DetectProgramDir ProgramDir

echo ProgramDir=%ProgramDir%

@rem copy compiler\mr_helper.mrp compiler\mr_helpere.mrp

set CurDir=%~dp0
set ProjDir=%CurDir:~0,-1%

set old_sys_include="%include%"
set old_sys_lib="%lib%"
set old_sys_path="%path%"

set CurDir=%~dp0
set ProjDir=%CurDir:~0,-1%

set MrpHomeDir=%ProjDir%\..\

set PERL5LIB=%PERL5LIB%
set PerlPath=%ProgramDir%\Perl\bin
set NASMPath=%ProgramDir%\nasm\bin
set YASMPath=%ProgramDir%\yasm\bin
set GPERFPath=%ProgramDir%\gperf\bin
set CMakePath=%ProgramDir%\cmake\bin
set SDCCPath=%ProgramDir%\SDCC\bin
set MakePath=%ProgramDir%\make-3.81-bin\bin
set PythonHome=%ProgramDir%\python
set PATH=%NASMPath%;%YASMPath%;%GPERFPath%;%PerlPath%;%CMakePath%;%SDCCPath%;%MakePath%;%PythonHome%;%PythonHome%\Scripts;%PATH%

set MakeProgram=%MakePath%\make.exe

call :TaskKillSpecProcess  "cl.exe"
call :TaskKillSpecProcess  "MSBuild.exe"

set BuildDir=BuildLib
set BuildType=Debug
set BuildType=Release
set ProjName=QtLang

@rem call :get_suf_sub_str %ProjDir% \ ProjName

set software_dir="%ProjDir%\thirdparty"
set HomeDir=%ProjDir%\out\windows
@rem set HomeDir=%ProgramDir%

call :SetProjEnv "%software_dir%" "%CurDir%" include lib path CMAKE_INCLUDE_PATH CMAKE_LIBRARY_PATH CMAKE_MODULE_PATH
call :ShowProjEnv

set SystemBinDir=.\

@rem x86  or x64
call %VisualStudioCmd% x64

@rem call "C:\Qt\6.5.2\msvc2019_64\bin\qtenv2.bat"
@rem call "D:/Qt/Qt5.12.0/5.12.0/msvc2017_64/bin/qtenv2.bat"
call "C:\Qt\6.9.0\msvc2022_64\bin\qtenv2.bat"
pushd %CurDir%

@rem Win32  or x64
set ArchType=x64

set BuildType=Release
set ProjName=

set all_srcs=src\main.c
set objs_dir=out

set lib_root_dir=out\windows\pcre-8.42
set lib_dir=%lib_root_dir%\lib
set inc_dir=%lib_root_dir%\include
set lib_name=%lib_dir%\pcred.lib

@rem call :CompileAllSrcs     "%all_srcs%"   "%objs_dir%"
@rem call :search_func_in_lib "%lib_name%"   "pcre_config"
@rem call :search_func_in_lib "main.obj"     "pcre_config"

set AllQtUis=%AllQtUis% %ProjDir%/src/mainwindow.ui
call :ProcQtUis       "%AllQtUis%"

set AllQtRcs=%ProjDir%/src/mainwindow.qrc
call :ProcQtRcs   "%AllQtRcs%"

set ProjDepDefs= -DWIN32 -DUNICODE -D_UNICODE -D_WIN64 -DWIN64 -D_ENABLE_EXTENDED_ALIGNED_STORAGE   -DLUA_STATIC 

set QtDepDefs=%QtDepDefs% -DQT_CONCURRENT_LIB    -DQT_CORE5COMPAT_LIB    -DQT_CORE_LIB      -DQT_GUI_LIB         -DQT_NETWORK_LIB -DQT_OPENGL_LIB      
set QtDepDefs=%QtDepDefs% -DQT_PRINTSUPPORT_LIB  -DQT_QMLINTEGRATION_LIB -DQT_QMLMODELS_LIB -DQT_QML_LIB         -DQT_QUICK_LIB   -DQT_WEBCHANNEL_LIB 
set QtDepDefs=%QtDepDefs% -DQT_WEBENGINECORE_LIB -DQT_WIDGETS_LIB        -DQT_XML_LIB       -DQT_POSITIONING_LIB -DQT_NO_CONTEXTLESS_CONNECT

set ProjIncDirs=%ProjIncDirs% -I%ProjDir%/src 
set ProjIncDirs=%ProjIncDirs% -I%ProjDir%/thirdparty/lua-5.4.4 
set ProjIncDirs=%ProjIncDirs% -I%ProjDir%/thirdparty/lua-5.4.4/include 
set ProjIncDirs=%ProjIncDirs% -I%ProjDir%/thirdparty/lua-5.4.4/src 

set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtCore 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/mkspecs/win32-msvc 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtCore5Compat 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtGui 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtWidgets 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtConcurrent 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtNetwork 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtPrintSupport 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtXml 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtWebEngineCore 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtQuick 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtQml 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtQmlIntegration 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtQmlModels 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtOpenGL 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtWebChannel 
set QtIncDirs=%QtIncDirs% -IC:/Qt/6.9.0/msvc2022_64/include/QtPositioning 

set AllQtHdrs=%AllQtHdrs% %ProjDir%/src/license.h
set AllQtHdrs=%AllQtHdrs% %ProjDir%/src/mainwindow.h
set AllQtHdrs=%AllQtHdrs% %ProjDir%/src/pcinfo.h
@rem set AllQtHdrs=%AllQtHdrs% %ProjDir%/src/websocketserver.h

call :ProcQtHdrs      "%ProjDepDefs%"   "%QtDepDefs%"    "%ProjIncDirs%"  "%QtIncDirs%"  "%AllQtHdrs%"

set ProjIncDirs=%ProjIncDirs% /I"%ProjDir%\src"
set ProjIncDirs=%ProjIncDirs% /I"%ProjDir%\out\win64\openssl-master\include"
set ProjIncDirs=%ProjIncDirs% /I"%ProjDir%\out\win64\libusb-win32-src-1.2.6.0\include"

set ProjDepDefs=%ProjDepDefs% /D WIN64 /D _WIN64 /D UNICODE /D _UNICODE /D WIN32 /D _WINDOWS  
set ProjDepDefs=%ProjDepDefs% /D _ENABLE_EXTENDED_ALIGNED_STORAGE   /D LUA_STATIC

set QtDepDefs=%QtDepDefs% /D QT_CORE_LIB /D QT_CORE5COMPAT_LIB   /D QT_PRINTSUPPORT_LIB /D QT_POSITIONING_LIB 
set QtDepDefs=%QtDepDefs% /D QT_GUI_LIB  /D QT_WIDGETS_LIB       /D QT_CONCURRENT_LIB   /D QT_NETWORK_LIB 
set QtDepDefs=%QtDepDefs% /D QT_XML_LIB  /D QT_WEBENGINECORE_LIB /D QT_QUICK_LIB        /D QT_QMLINTEGRATION_LIB 
set QtDepDefs=%QtDepDefs% /D QT_QML_LIB  /D QT_QMLMODELS_LIB     /D QT_OPENGL_LIB       /D QT_WEBCHANNEL_LIB 
@rem set QtDepDefs=%QtDepDefs% /D QT_NO_CONTEXTLESS_CONNECT

@rem set QtDepDefs=%QtDepDefs% /D "CMAKE_INTDIR=\"Debug\"" 

set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtCore" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include"
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/mkspecs/win32-msvc" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtCore5Compat"
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtGui" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtWidgets" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtConcurrent" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtNetwork" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtPrintSupport" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtXml" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtWebEngineCore" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtQuick" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtQml" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtQmlIntegration" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtQmlModels" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtOpenGL" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtWebChannel" 
set QtIncDirs=%QtIncDirs% /external:I "C:/Qt/6.9.0/msvc2022_64/include/QtPositioning" 

@rem set ProjAllSrcs=%ProjDir%\out\Debug\qrc_mainwindow.cpp 
set ProjAllSrcs=%ProjAllSrcs% out\Debug\*.cpp 
set ProjAllSrcs=%ProjAllSrcs% %ProjDir%\src\main.cpp 
set ProjAllSrcs=%ProjAllSrcs% %ProjDir%\src\license.cpp 
set ProjAllSrcs=%ProjAllSrcs% %ProjDir%\src\mainwindow.cpp 
set ProjAllSrcs=%ProjAllSrcs% %ProjDir%\src\pcinfo.cpp 
@rem set ProjAllSrcs=%ProjAllSrcs% %ProjDir%\src\websocketserver.cpp 
@rem set ProjAllSrcs=%ProjAllSrcs% qrc_mainwindow.cpp
call :CompileQtSrcs   "%ProjIncDirs%"   "%ProjDepDefs%"  "%QtDepDefs%"    "%QtIncDirs%"    "%ProjAllSrcs%"

set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/lib" 
set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/lib/Debug" 
set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/thirdparty/lua-5.4.4" 
set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/thirdparty/lua-5.4.4/Debug" 
set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/thirdparty/lua-5.4.4/lib" 
set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/thirdparty/lua-5.4.4/lib/Debug" 
set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/thirdparty/lua-5.4.4/src" 
set ProjLibDirs=%ProjLibDirs% /LIBPATH:"%ProjDir%/thirdparty/lua-5.4.4/src/Debug" 


set DepQtLibs=%ProjLibs%
@rem set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Core5Compatd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Concurrentd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6PrintSupportd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Xmld.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6WebEngineCored.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Widgetsd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Quickd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6QmlModelsd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6OpenGLd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Guid.lib 
@rem set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6WebChanneld.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Qmld.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Networkd.lib 
@rem set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Positioningd.lib 
set DepQtLibs=%DepQtLibs% C:\Qt\6.9.0\msvc2022_64\lib\Qt6Cored.lib 

set DepSysLibs=%DepSysLibs% comdlg32.lib winspool.lib d3d11.lib dxgi.lib dxguid.lib dcomp.lib user32.lib 
set DepSysLibs=%DepSysLibs% ws2_32.lib shell32.lib mpr.lib userenv.lib kernel32.lib user32.lib gdi32.lib 
set DepSysLibs=%DepSysLibs% winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib 
set DepSysLibs=%DepSysLibs% "%ProjDir%\out\win64\openssl-master\lib\libcrypto-1_1-x64.lib"
set DepSysLibs=%DepSysLibs% "%ProjDir%\out\win64\openssl-master\lib\libssl-1_1-x64.lib"

set ProjAllObjs=out\Debug\*.obj
set ProjAllObjs=%ProjAllObjs% out\Debug\main.obj
set ProjAllObjs=%ProjAllObjs% out\Debug\license.obj
set ProjAllObjs=%ProjAllObjs% out\Debug\mainwindow.obj
set ProjAllObjs=%ProjAllObjs% out\Debug\pcinfo.obj
@rem set ProjAllObjs=%ProjAllObjs% out\Debug\websocketserver.obj
@rem set ProjAllObjs=%ProjAllObjs% out\Debug\qrc_mainwindow.obj
call :LinkQtObjs      "%ProjLibDirs%"   "%DepQtLibs%"    "%DepSysLibs%"   "%ProjAllObjs%" 

pause
goto :eof

:del_lib_cacke_dir
    setlocal EnableDelayedExpansion
    set lib_dir="%~1"
    set home_dir="%~2"
    call :color_text 2f "++++++++++++++ del_lib_cacke_dir ++++++++++++++"
    pushd %lib_dir%
        set idx=0
        for /f %%i in ( 'dir /b /ad ' ) do (
            set /a idx+=1
            set cur_lib_name=%%i
            echo [!idx!] !cur_lib_name!
            if exist !cur_lib_name!\dyzbuild (
                echo !cur_lib_name!\dyzbuild does exist
                pause
            )
            if exist !cur_lib_name!\SMP\.vs (
                echo !cur_lib_name!\SMP\.vs does exist
                pause
            )
            if exist !cur_lib_name!\SMP\obj (
                echo !cur_lib_name!\SMP\obj does exist
                pause
            )
            tar -caf !cur_lib_name!.tar.gz !cur_lib_name!
        )
    popd
    call :color_text 2f " -------------- del_lib_cacke_dir --------------- "
    endlocal
goto :eof

:TaskKillSpecProcess
    setlocal EnableDelayedExpansion
    set ProcName=%~1
    call :color_text 2f " +++++++++++++++++++ TaskKillSpecProcess +++++++++++++++++++ "
    tasklist | grep  "%ProcName%"
    taskkill /f /im  "%ProcName%"
    call :color_text 2f " -------------------- TaskKillSpecProcess ----------------------- "
    endlocal
goto :eof

:upgrade_python_pip
    setlocal EnableDelayedExpansion
    python -m ensurepip
    python -m pip install --upgrade pip
    pip3 install Jinja2
    call :color_text 2f " -------------------- upgrade_python_pip ----------------------- "
    endlocal
goto :eof

:DetectProgramDir
    setlocal EnableDelayedExpansion
    @rem SkySdk\VS2005\VC
    set SkySdkDiskSet=C;D;E;F;G;
    set CurProgramDir=
    set idx=0
    call :color_text 2f " +++++++++++++++++++ DetectProgramDir +++++++++++++++++++++++ "
    for %%i in (%SkySdkDiskSet%) do (
        set /a idx+=1
        for /f "tokens=1-2 delims=|" %%B in ("programs|program") do (
            set CurProgramDir=%%i:\%%B
            echo [!idx!] !CurProgramDir!
            if exist !CurProgramDir!\SkySdk (
                goto :DetectProgramDirBreak
            )
            set CurProgramDir=%%i:\%%C
            echo [!idx!] !CurProgramDir!
            if exist !CurProgramDir!\SkySdk (
                goto :DetectProgramDirBreak
            )
        )
    )
    :DetectProgramDirBreak
    set ProgramDir=!CurProgramDir!
    call :color_text 2f " ------------------- DetectProgramDir ----------------------- "
    endlocal & set %~1=%ProgramDir%
goto :eof

:CheckLibInDir
    setlocal EnableDelayedExpansion
    set Libs=%~1
    set LibDir="%~2"
    set ProjDir=%~3
    set MyPlatformSDK=%ProjDir%\lib
    if not exist "%MyPlatformSDK%" (
        mkdir %MyPlatformSDK%
    )
    call :color_text 2f " +++++++++++++++++++ CheckLibInDir +++++++++++++++++++++++ "
    echo LibDir %LibDir%
    if not exist %LibDir% (
        call :color_text 4f " -------------------- CheckLibInDir ----------------------- "
        echo '%LibDir%' does not exist... 
        goto :eof
    )

    pushd %LibDir%
    set idx=0
    for %%i in (%Libs%) do (
        set /a idx+=1
        set CurLib=%%i
        echo [!idx!] !LibDir!\!CurLib!
        if not exist !LibDir!\!CurLib! (
            echo !LibDir!\!CurLib!
        ) else (
            copy !LibDir!\!CurLib! %MyPlatformSDK%
        )
    )
    popd
    call :color_text 2f " -------------------- CheckLibInDir ----------------------- "
    endlocal
goto :eof

:DetectVsPath
    setlocal EnableDelayedExpansion
    set VsBatFileVar=%~1
    call :color_text 2f " ++++++++++++++++++ DetectVsPath +++++++++++++++++++++++ "
    set VSDiskSet=C;D;E;F;G;

    set AllProgramsPathSet=program
    set AllProgramsPathSet=%AllProgramsPathSet%;programs
    set AllProgramsPathSet=%AllProgramsPathSet%;"Program Files"
    set AllProgramsPathSet=%AllProgramsPathSet%;"Program Files (x86)"

    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build"
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build"
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build"
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build"
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build"
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build"
    set VCPathSet=%VCPathSet%;SkySdk\VS2005\VC
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio 8\VC"
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio 12.0\VC\bin"
    set VCPathSet=%VCPathSet%;"Microsoft Visual Studio 14.0\VC\bin"

    set idx_a=0
    for %%C in (%VCPathSet%) do (
        set /a idx_a+=1
        set idx_b=0
        for %%B in (!AllProgramsPathSet!) do (
            set /a idx_b+=1
            set idx_c=0
            for %%A in (!VSDiskSet!) do (
                set /a idx_c+=1
                set CurBatFile=%%A:\%%B\%%C\vcvarsall.bat
                echo [!idx_a!][!idx_b!][!idx_c!] !CurBatFile!
                if exist !CurBatFile! (
                    goto DetectVsPathBreak
                )
            )
        )
    )
    :DetectVsPathBreak
    echo Use:%CurBatFile%
    call :color_text 2f " -------------------- DetectVsPath ----------------------- "
    endlocal & set "%~1=%CurBatFile%"
goto :eof

@rem call :ProcQtUis   "%AllQtUis%"
:ProcQtUis
    setlocal EnableDelayedExpansion
    set AllQtUis=%~1
    call :color_text 2f " +++++++++++++++++ ProcQtUis ++++++++++++++++ "
    set OutDir=out\Debug
    if not exist %OutDir% (
        mkdir %OutDir%
    )
    set idx=0
    set AllUiHdrs=
    for %%i in (%AllQtUis%) do (
        set /a idx+=1
        set QtUiFile=%%i
        set QtUiHdr=!OutDir!\ui_%%~ni.h
        set ext=%%~xi
        echo [!idx!] !QtUiFile!  !QtUiHdr!
        set AllUiHdrs=!AllUiHdrs! !QtUiHdr!
        uic.exe -o  !QtUiHdr!  !QtUiFile!
    )
    call :color_text 2f " ----------------- ProcQtUis ---------------- "
    endlocal
goto :eof

@rem call :ProcQtRcs   "%AllQtRcs%"
:ProcQtRcs
    setlocal EnableDelayedExpansion
    set AllQtRcs=%~1

    call :color_text 2f " +++++++++++++++++ ProcQtRcs ++++++++++++++++ "
    set OutDir=out\Debug
    if not exist %OutDir% (
        mkdir %OutDir%
    )
    set idx=0
    set AllQtCpps=
    for %%i in (%AllQtRcs%) do (
        set /a idx+=1
        set QrcFile=%%i
        set FileName=%%~ni
        set CppFile=!OutDir!\qrc_!FileName!_CMKE_.cpp
        set ext=%%~xi
        echo [!idx!] !QrcFile!  !CppFile!
        set AllQtCpps=!AllQtCpps! !CppFile!
        rcc.exe -name !FileName! -o !CppFile! !QrcFile!
    )

    call :color_text 2f " ----------------- ProcQtRcs ---------------- "
    endlocal
goto :eof

@rem call :ProcQtHdrs   "%ProjDepDefs%"   "%QtDepDefs%"   "%ProjIncDirs%"   "%QtIncDirs%"   "%AllQtHdrs%"
:ProcQtHdrs
    setlocal EnableDelayedExpansion
    set ProjDepDefs=%~1
    set QtDepDefs=%~2
    set ProjIncDirs=%~3
    set QtIncDirs=%~4
    set AllQtHdrs=%~5

    call :color_text 2f " +++++++++++++++++ ProcQtHdrs ++++++++++++++++ "

    echo ProjDepDefs=%ProjDepDefs%
    echo QtDepDefs=%QtDepDefs%
    echo ProjIncDirs=%ProjIncDirs%
    echo QtIncDirs=%QtIncDirs%
    echo AllQtHdrs=%AllQtHdrs%

    set OutDir=out\Debug
    if not exist %OutDir% (
        mkdir %OutDir%
    )
    set idx=0
    set AllMocCpps=
    for %%i in (%AllQtHdrs%) do (
        set /a idx+=1
        set QtHdrFile=%%i
        set QtMocCpp=!OutDir!\moc_%%~ni.cpp
        set ext=%%~xi
        echo [!idx!] !QtHdrFile!  !QtMocCpp!
        set AllMocCpps=!AllMocCpps! !QtMocCpp!
        moc.exe !ProjDepDefs!   !QtDepDefs!   !ProjIncDirs!   !QtIncDirs! --output-dep-file -o   !QtMocCpp!   !QtHdrFile!
    )
    call :color_text 2f " ----------------- ProcQtHdrs ---------------- "
    endlocal
goto :eof



@rem call :CompileQtSrcs   "%ProjIncDirs%"   "%ProjDepDefs%"   "%QtDepDefs%"   "%QtIncDirs%"    "%ProjAllSrcs%"
:CompileQtSrcs
    setlocal EnableDelayedExpansion
    set ProjIncDirs=%~1
    set ProjDepDefs=%~2
    set QtDepDefs=%~3
    set QtIncDirs=%~4
    set ProjAllSrcs=%~5

    call :color_text 2f " +++++++++++++++++ CompileQtSrcs ++++++++++++++++ "

    echo ProjIncDirs=%ProjIncDirs%
    echo ProjDepDefs=%ProjDepDefs%
    echo QtDepDefs=%QtDepDefs%
    echo QtIncDirs=%QtIncDirs%
    echo ProjAllSrcs=%ProjAllSrcs%

    set OutDir=out\Debug
    set ZcOpts=/Zc:wchar_t /Zc:forScope /Zc:inline

    set idx=0
    set AllMocCpps=
    for %%i in (%ProjAllSrcs%) do (
        set /a idx+=1
        set QtCppFile=%%i
        set ObjFile=!OutDir!\%%~ni.obj
        set ext=%%~xi
        echo [!idx!] !QtCppFile!  !ObjFile!
        set AllObjFiles=!AllObjFiles! !ObjFile!
        echo CL.exe /c  /I"!OutDir!" !ProjIncDirs! /Z7 /nologo /W4 /WX- /diagnostics:column /Od /Ob0 !ProjDepDefs! !QtDepDefs! /Gm- /EHsc /RTC1 /MDd /GS /fp:precise !ZcOpts! /GR /std:c++17 /permissive- /Fo"!OutDir!\\" /Fd"!OutDir!\vc143.pdb" /external:W0 /Gd /TP /errorReport:queue  !QtIncDirs! -Zc:__cplusplus -utf-8 !QtCppFile!
        CL.exe /c  /I"!OutDir!" !ProjIncDirs! /Z7 /nologo /W4 /WX- /diagnostics:column /Od /Ob0 !ProjDepDefs! !QtDepDefs! /Gm- /EHsc /RTC1 /MDd /GS /fp:precise !ZcOpts! /GR /std:c++17 /permissive- /Fo"!OutDir!\\" /Fd"!OutDir!\vc143.pdb" /external:W0 /Gd /TP /errorReport:queue  !QtIncDirs! -Zc:__cplusplus -utf-8 !QtCppFile!
    )
    call :color_text 2f " ----------------- CompileQtSrcs ---------------- "
    endlocal
goto :eof



@rem call :LinkQtObjs   "%ProjLibDirs%"   "%QtLibDirs%"   "%DepSysLibs%"   "%ProjAllObjs%"    
:LinkQtObjs
    setlocal EnableDelayedExpansion
    set ProjLibDirs=%~1
    set QtLibDirs=%~2
    set DepSysLibs=%~3
    set ProjAllObjs=%~4

    set TargetName=QtLang

    call :color_text 2f " +++++++++++++++++ LinkQtObjs ++++++++++++++++ "

    echo ProjLibDirs=%ProjLibDirs%
    echo QtLibDirs=%QtLibDirs%
    echo DepSysLibs=%DepSysLibs%
    echo ProjAllObjs=%ProjAllObjs%

    set OutDir=out\Debug

    link.exe /ERRORREPORT:QUEUE /OUT:"%OutDir%\%TargetName%.exe" /INCREMENTAL /ILK:"%OutDir%\%TargetName%.ilk" /NOLOGO %ProjLibDirs% %QtLibDirs% %DepSysLibs% /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"%OutDir%/%TargetName%.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"%OutDir%/%TargetName%.lib" /MACHINE:X64 %ProjAllObjs%

    call :color_text 2f " ----------------- LinkQtObjs ---------------- "
    endlocal
goto :eof

:CompileQtSln
    setlocal EnableDelayedExpansion
    set QtSln=%~1
    call :color_text 2f " +++++++++++++++++ CompileQtSln ++++++++++++++++ "
    set OutDir=out\Debug

    call :color_text 2f " ----------------- CompileQtSln ---------------- "
    endlocal
goto :eof

:CompileAllSrcs
    setlocal EnableDelayedExpansion
    set AllSrcs=%~1
    set ObjsDir="%~2"
    set PcreDir=%HomeDir%\pcre-8.42
    call :color_text 2f " ++++++++++++++++++ CompileAllSrcs ++++++++++++++++++++++++ "
    set IncDir=/I .\inc    /I .\src    /I  .\    /I  %PcreDir%\include
    set PcreDefs=/D PCRE_STATIC
    set WinDefs=/D _WINDOWS  /D WIN32  /D _NDEBUG  /D NDEBUG  
    set AllDefs=%PcreDefs%  /TC  /MD
    set OutDir=out
    if not exist %OutDir% (
        mkdir %OutDir%
    )
    set idx=0
    set AllObjs=
    for %%i in (%AllSrcs%) do (
        set /a idx+=1
        set srcFile=%%i
        set objFile=%OutDir%\%%~ni.obj
        set ext=%%~xi
        echo [!idx!] !srcFile!  !objFile!
        set AllObjs=!AllObjs! !objFile!
        echo cl  %IncDir%  %AllDefs%  /Fo"!objFile!"  /c  !srcFile!
        cl  %IncDir%  %AllDefs%  /Fo"!objFile!"  /c  !srcFile!
    )

    set LibDir=/LIBPATH:.\    /LIBPATH:.\lib    /LIBPATH:%PcreDir%\lib
    set PcreOpts=pcred.lib 
    set WinOpts=/subsystem:windows    /entry:WinMainCRTStartup
    set ArmOpts=/SAFESEH:NO    /NODEFAULTLIB:LIBCD.lib    /NODEFAULTLIB:LIBC.lib    /NODEFAULTLIB:LIBCMT.lib    /NODEFAULTLIB:LIBCMTD.lib    /INCLUDE:_sim_dummy    MSCOREE.lib
    set AllOpts=%PcreOpts%
    echo     link    /link    %LibDir%    %AllOpts%    %AllObjs% /OUT:%OutDir%\app.exe
    link    /link    %LibDir%    %AllOpts%    %AllObjs% /OUT:%OutDir%\app.exe
    call :color_text 2f " ----------------- CompileAllSrcs ----------------- "
    endlocal
goto :eof

:search_func_in_lib
    setlocal ENABLEDELAYEDEXPANSION
    set lib_name=%1
    set func_name=%~2
    call:color_text 2f "++++++++++++++++++dumpbin SYMBOLS lib_name++++++++++++++++++"
    @rem dumpbin /?
    @rem dumpbin /ARCHIVEMEMBERS  %lib_name%
    @rem dumpbin /HEADERS %lib_name%  /SECTION:.drectve
    dumpbin /HEADERS %lib_name%  | grep "machine"
    dumpbin /SYMBOLS %lib_name%  | grep "%func_name%"
    call:color_text 2f "------------------dumpbin SYMBOLS lib_name------------------"
    endlocal
goto :eof

:SetProjEnv
    setlocal ENABLEDELAYEDEXPANSION
    set software_dir=%~1
    set loc_dir=%~2
    set loc_inc=%3
    set loc_lib=%4
    set loc_bin=%5
    call :color_text 9f " ++++++++++++++ SetProjEnv ++++++++++++++ "
    if not exist %software_dir% (
        echo software_dir '%software_dir%' doesn't exist!
        goto :eof
    )
    if not exist %HomeDir% (
        echo HomeDir '%HomeDir%' doesn't exist!
        goto :eof
    )
    call :gen_all_env_by_dir %software_dir% %HomeDir% loc_inc loc_lib loc_bin cmake_inc cmake_lib cmake_path
    set all_inc=%loc_inc%;%include%;%loc_dir%\include;
    set all_lib=%loc_lib%;%lib%;%loc_dir%\lib;%loc_dir%\bin;
    set all_bin=%loc_bin%;%path%;%loc_dir%\bin;
    call :color_text 9f " -------------- SetProjEnv -------------- "
    endlocal & set %~3=%all_inc% & set %~4=%all_lib% & set %~5=%all_bin% & set %~6=%cmake_inc% & set %~7=%cmake_lib% & set %~8=%cmake_path%
goto :eof

:ShowProjEnv
    setlocal ENABLEDELAYEDEXPANSION
    call :color_text 9f " ++++++++++++++ ShowProjEnv ++++++++++++++ "
    echo include           :%include%
    echo lib               :%lib%
    echo path              :%path%
    echo CMAKE_INCLUDE_PATH:%CMAKE_INCLUDE_PATH%
    echo CMAKE_LIBRARY_PATH:%CMAKE_LIBRARY_PATH%
    echo CMAKE_MODULE_PATH :%CMAKE_MODULE_PATH%
    call :color_text 9f " -------------- ShowProjEnv -------------- "
    endlocal
goto :eof

:ResetSystemEnv
    setlocal ENABLEDELAYEDEXPANSION
    call :color_text 9f "++++++++++++++ResetSystemEnv++++++++++++++"
    @rem set include=%old_sys_include%
    @rem set lib=%old_sys_lib%
    @rem set path=%old_sys_path%
    call :color_text 9f "--------------ResetSystemEnv--------------"
    endlocal
goto :eof

@rem YellowBackground    6f  ef
@rem BlueBackground      9f  bf   3f
@rem GreenBackground     af  2f
@rem RedBackground       4f  cf
@rem GreyBackground      7f  8f
@rem PurpleBackground    5f

:color_text
    setlocal EnableDelayedExpansion
    for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
        set "DEL=%%a"
    )
    echo off
    <nul set /p ".=%DEL%" > "%~2"
    findstr /v /a:%1 /R "^$" "%~2" nul
    del "%~2" > nul 2>&1
    endlocal
    echo .
goto :eof

:find_lib_by_func
    setlocal ENABLEDELAYEDEXPANSION
    set lib_dir=%1
    set func_name=%~2
    call:color_text 2f "++++++++++++++find_lib_by_func++++++++++++++"
    set idx=0
    pushd %lib_dir%
        for /f %%i in (' dir /b  ') do (
            set /a idx+=1
            set lib_file=%%i
            echo [!idx!]lib_file:!lib_file! 
            call :search_func_in_lib "!lib_file!"  "%func_name%"
        )
    popd
    call:color_text 9f "--------------find_lib_by_func--------------"
    endlocal
goto :eof

:search_func_in_lib
    setlocal ENABLEDELAYEDEXPANSION
    set lib_name=%1
    set func_name=%~2
    if "%func_name%" == "" (
        dumpbin /EXPORTS %lib_name%
        dumpbin /SYMBOLS %lib_name%
        strings %lib_name%
    ) else (
        dumpbin /SYMBOLS %lib_name%  | grep %func_name%
        strings %lib_name% |  findstr /i "%func_name%"
    )
    endlocal
goto :eof

:show_lib_info
    setlocal ENABLEDELAYEDEXPANSION
    set lib_name=%1
    set spec_obj=%~2
    set sym_text=%lib_name%_sym.txt
    call:color_text 2f "++++++++++++++show_lib_info++++++++++++++"
    echo %0 %lib_name%
    @rem objdump -S %lib_name% | grep -C 5 "_open"
    lib /list %lib_name% > %sym_text%
    lib /list:%sym_text% %lib_name%
    echo %0 %lib_name%  %sym_text%
    set idx=0
    @rem for /f "tokens=4 delims=," %%i in ( %sym_text% ) do (
    for /f %%i in ( %sym_text% ) do (
        set /a idx+=1
        set obj_file=%%i
        echo [!idx!]obj_file:!obj_file!  lib_name:!lib_name!  spec_obj: !spec_obj!
        if "!obj_file!" == "!spec_obj!" (
            lib !lib_name! /extract:!obj_file!
        )
    )
    call:color_text 9f "--------------show_lib_info--------------"
    endlocal
goto :eof

:show_obj_info
    setlocal ENABLEDELAYEDEXPANSION
    set spec_obj=%~1

    call:color_text 2f "++++++++++++++show_obj_info++++++++++++++"
    echo %0 %spec_obj%
    set idx=0
    :GOON
    for /f "delims=\, tokens=1,*" %%i in ("%spec_obj%") do (
        set /a idx+=1
        echo [!idx!]%%i %%j
        set spec_obj=%%j
        set file_name=%%i
        echo [!idx!]file_name:!file_name! spec_obj: !spec_obj!
        if exist !spec_obj! (
            dumpbin /all !spec_obj! > !spec_obj!_sym.txt
            dumpbin /disasm !spec_obj! > !spec_obj!_asm.txt 
            goto :GOON_END
        )
        goto GOON
    )
    :GOON_END
    call:color_text 9f "--------------show_obj_info--------------"
    endlocal
goto :eof

:append_obj_to_lib
    setlocal ENABLEDELAYEDEXPANSION
    lib simulator.lib os_adapter.obj
    lib /out:simulator_new.lib simulator.lib os_adapter.obj
    @rem lib simulator.lib /remove os_adapter.obj
    endlocal
goto :eof

:MakeDir
    setlocal EnableDelayedExpansion
    set DirName="%~1"
    if not exist %DirName% (
        mkdir %DirName%
    )
    endlocal
goto :eof

:get_path_by_file
    setlocal EnableDelayedExpansion
    set myfile=%1
    set mypath=%~dp1
    set myname=%~n1
    set myext=%~x1
    call :color_text 2f "++++++++++++++++++ get_path_by_file ++++++++++++++++++++++++"
    echo !mypath! !myname! !myext!
    call :color_text 2f "-------------------- get_path_by_file -----------------------"
    endlocal & set %~2=%mypath%&set %~3=%myname%&set %~4=%myext%
goto :eof

:get_dir_by_tar
    setlocal ENABLEDELAYEDEXPANSION
    set tar_file="%~1"
    call :color_text 2f "++++++++++++++get_dir_by_tar++++++++++++++"
    @rem for /f "tokens=8 delims= " %%i in ('tar -tf %tar_file%') do ( echo %%~i )
    set FileDir=
    set file_name=
    echo "    tar -tf %tar_file% | grep "/$" | gawk -F"/" "{ print $1 }" | sed -n "1p"    "
    FOR /F "usebackq" %%i IN (` tar -tf %tar_file% ^| grep "/$" ^| gawk -F"/" "{print $1}" ^| sed -n "1p" `) DO (set FileDir=%%i)
    @rem echo tar_file:%tar_file% FileDir:!FileDir!
    call :is_contain %tar_file% %FileDir% file_name
    if "%file_name%" == "false" (
        call :color_text 4f "-------------get_dir_by_tar--------------"
        echo tar_file:%tar_file% FileDir:%FileDir%
    )
    endlocal & set %~2=%FileDir%
goto :eof

:get_dir_by_zip
    setlocal ENABLEDELAYEDEXPANSION
    set zip_file="%~1"
    call :color_text 2f "++++++++++++++get_dir_by_zip++++++++++++++"
    @rem for /f "tokens=8 delims= " %%i in ('unzip -v %zip_file%') do ( echo %%~i )
    set FileDir=
    set file_name=
    @rem unzip -v %zip_file% | gawk -F" "  "{ print $8 } " | gawk  -F"/" "{ print $1 }" | sed -n "5p"
    echo zip_file:%zip_file%
    FOR /F "usebackq" %%i IN (` unzip -v %zip_file% ^| gawk -F" "  "{ print $8 } " ^| gawk  -F"/" "{ print $1 }" ^| sed -n "5p" `) DO (set FileDir=%%i)
    FOR /F "usebackq" %%i IN (` unzip -v %zip_file% ^| gawk -F" "  "{print $8}" ^| grep -v "/[^$]" ^| gawk  -F"/" "{ print $1 }" ^| sed -n "5p" `) DO (set FileDir=%%i)
    @rem echo zip_file:%zip_file% FileDir:!FileDir!
    call :is_contain %zip_file% %FileDir% file_name
    if "%file_name%" == "false" (
        call :color_text 4f "-------------get_dir_by_zip--------------"
        echo zip_file:%zip_file% FileDir:%FileDir%
        pause
    )
    endlocal & set %~2=%FileDir%
goto :eof

:gen_env_by_file
    setlocal ENABLEDELAYEDEXPANSION
    set zip_file="%~1"
    set HomeDir=%~2
    set FileDir=
    call :color_text 9f " ++++++++++++++ gen_env_by_file ++++++++++++++ "
    call :get_pre_sub_str !zip_file! . file_name
    call :get_last_char_pos !zip_file! . ext_name_pos
    echo file_name:!file_name! ext_name_pos:!ext_name_pos!
    call :get_suf_sub_str !zip_file! . ext_name
    echo ext_name:!ext_name!
    if "%ext_name%" == "zip" (
        call :get_dir_by_zip %zip_file% FileDir
    ) else if "%ext_name%" == "gz" (
        call :get_dir_by_tar %zip_file% FileDir
    ) else if "%ext_name%" == "xz" (
        call :get_dir_by_tar %zip_file% FileDir
    ) else (
        echo "%ext_name%"
    )
    call :color_text 9f " -------------- gen_env_by_file -------------- "
    set DstDirWithHome=%HomeDir%\%FileDir%
    echo %0 %zip_file% %DstDirWithHome%
    endlocal & set %~3=%DstDirWithHome%
goto :eof

:gen_all_env_by_file
    setlocal ENABLEDELAYEDEXPANSION
    set thridparty_dir="%~1"
    set home_dir="%~2"
    set DstDirWithHome=
    call :color_text 2f " ++++++++++++++ gen_all_env_by_file ++++++++++++++ "
    if not exist %thridparty_dir% (
        echo Dir '%thridparty_dir%' doesn't exist!
        goto :eof
    )
    pushd %thridparty_dir%
        for /f %%i in ( 'dir /b *.tar.* *.zip' ) do (
            set tar_file=%%i
            call :gen_env_by_file !tar_file! !home_dir! DstDirWithHome
            set inc=!DstDirWithHome!\include;!inc!
            set lib=!DstDirWithHome!\lib;!lib!
            set bin=!DstDirWithHome!\bin;!bin!
            set CMAKE_INCLUDE_PATH=!DstDirWithHome!\include;!CMAKE_INCLUDE_PATH!
            set CMAKE_LIBRARY_PATH=!DstDirWithHome!\lib;!CMAKE_LIBRARY_PATH!
            set CMAKE_MODULE_PATH=!DstDirWithHome!\lib\cmake;!CMAKE_MODULE_PATH!
            set CMAKE_MODULE_PATH=!DstDirWithHome!\cmake;!CMAKE_MODULE_PATH!
        )
    popd
    call :color_text 9f " -------------- gen_all_env_by_file -------------- "
    echo inc:%inc%
    echo lib:%lib%
    echo bin:%bin%
    endlocal & set %~3=%inc% & set %~4=%lib% & set %~5=%bin% & set %~6=%CMAKE_INCLUDE_PATH% & set %~7=%CMAKE_LIBRARY_PATH% & set %~8=%CMAKE_MODULE_PATH%
goto :eof

:gen_env_by_dir
    setlocal ENABLEDELAYEDEXPANSION
    set FileDir=%~1
    set HomeDir=%~2
    set DstDirWithHome=%3

    call :color_text 9f " ++++++++++++++ gen_env_by_dir ++++++++++++++ "
    set DstDirWithHome=%HomeDir%\%FileDir%
    echo %0 %zip_file% %DstDirWithHome%
    endlocal & set %~3=%DstDirWithHome%
goto :eof

:gen_all_env_by_dir
    setlocal ENABLEDELAYEDEXPANSION
    set thridparty_dir="%~1"
    set home_dir="%~2"
    set DstDirWithHome=
    call :color_text 2f " ++++++++++++ gen_all_env_by_dir ++++++++++++ "
    echo thridparty_dir  :%thridparty_dir%
    echo home_dir        :%home_dir%
    echo DstDirWithHome  :%DstDirWithHome%
    if not exist %thridparty_dir% (
        echo Dir '%thridparty_dir%' doesn't exist!
        goto :eof
    )
    pushd %thridparty_dir%
        for /f %%i in ( 'dir /b /ad ' ) do (
            set soft_dir=%%i
            call :gen_env_by_dir !soft_dir! !home_dir! DstDirWithHome
            set cur_inc=!DstDirWithHome!\include;!cur_inc!
            set cur_lib=!DstDirWithHome!\lib;!cur_lib!
            set cur_bin=!DstDirWithHome!\bin;!cur_bin!
            set CMAKE_INCLUDE_PATH=!DstDirWithHome!\include;!CMAKE_INCLUDE_PATH!
            set CMAKE_LIBRARY_PATH=!DstDirWithHome!\lib;!CMAKE_LIBRARY_PATH!
            set CMAKE_MODULE_PATH=!DstDirWithHome!\lib\cmake;!CMAKE_MODULE_PATH!
            set CMAKE_MODULE_PATH=!DstDirWithHome!\cmake;!CMAKE_MODULE_PATH!
        )
    popd
    call :color_text 9f " ----------- gen_all_env_by_dir ------------ "
    echo cur_inc    :%cur_inc%
    echo cur_lib    :%cur_lib%
    echo cur_bin    :%cur_bin%
    endlocal & set %~3=%cur_inc% & set %~4=%cur_lib% & set %~5=%cur_bin% & set %~6=%CMAKE_INCLUDE_PATH% & set %~7=%CMAKE_LIBRARY_PATH% & set %~8=%CMAKE_MODULE_PATH%
goto :eof

:show_all_env
    setlocal ENABLEDELAYEDEXPANSION
    call :color_text 2f " +++++++++++ show_all_env ++++++++++++ "
    echo include    :%include%
    echo lib        :%lib%
    echo path       :%path%
    echo CMAKE_INCLUDE_PATH     :%CMAKE_INCLUDE_PATH%
    echo CMAKE_LIBRARY_PATH     :%CMAKE_LIBRARY_PATH%
    echo CMAKE_MODULE_PATH      :%CMAKE_MODULE_PATH%
    call :color_text 2f " ----------- show_all_env ------------ "
    endlocal
goto :eof

:get_str_len
    setlocal ENABLEDELAYEDEXPANSION
    set mystr=%~1
    set mystrlen="%~2"
    set count=0
    call :color_text 2f "++++++++++++++get_str_len++++++++++++++"
    :intercept_str_len
    set /a count+=1
    for /f %%i in ("%count%") do (
        if not "!mystr:~%%i,1!"=="" (
            goto :intercept_str_len
        )
    )
    echo %0 %mystr% %count%
    endlocal & set %~2=%count%
goto :eof

:get_first_char_pos
    setlocal ENABLEDELAYEDEXPANSION
    set mystr=%~1
    set char_sym=%~2
    set char_pos="%~3"
    call :get_str_len %mystr% mystrlen
    set count=0
    call :color_text 2f "++++++++++++++get_first_char_pos++++++++++++++"
    :intercept_first_char_pos
    for /f %%i in ("%count%") do (
        set /a count+=1
        if not "!mystr:~%%i,1!"=="!char_sym!" (
            goto :intercept_first_char_pos
        )
    )
    echo %0 %mystr% %char_sym% %count%
    endlocal & set %~3=%count%
goto :eof

:get_last_char_pos
    setlocal ENABLEDELAYEDEXPANSION
    set mystr=%~1
    set char_sym=%~2
    set char_pos="%~3"
    call :get_str_len %mystr% mystrlen
    set count=%mystrlen%
    call :color_text 2f "++++++++++++++get_last_char_pos++++++++++++++"
    @rem set /a count-=1
    :intercept_last_char_pos
    for /f %%i in ("%count%") do (
        if not "!mystr:~%%i,1!"=="!char_sym!" (
            set /a count-=1
            goto :intercept_last_char_pos
        )
    )
    echo %0 %mystr% %char_sym% %count%
    endlocal & set %~3=%count%
goto :eof

:get_pre_sub_str
    setlocal ENABLEDELAYEDEXPANSION
    set mystr=%~1
    set char_sym=%~2
    set mysubstr="%~3"
    call :get_str_len %mystr% mystrlen
    set count=0
    call :color_text 2f "++++++++++++++get_pre_sub_str++++++++++++++"
    set substr=
    :intercept_pre_sub_str
    for /f %%i in ("%count%") do (
        set /a count+=1
        if not "!mystr:~%%i,1!"=="!char_sym!" (
            set /a mysubstr_len=%%i
            set substr=!mystr:~0,%%i!
            if "%count%" == "%mystrlen%" (
                goto :pre_sub_str_break
            )
            goto :intercept_pre_sub_str
        ) else (
            set /a mysubstr_len=%%i
            set substr=!mystr:~0,%%i!
            goto :pre_sub_str_break
        )
    )
    :pre_sub_str_break
    echo %0 %mystr% %char_sym% %count% %mysubstr_len%
    endlocal & set %~3=%substr%
goto :eof

:get_suf_sub_str
    setlocal ENABLEDELAYEDEXPANSION
    set mystr=%~1
    set char_sym=%~2
    set mysubstr="%~3"
    call :get_str_len %mystr% mystrlen
    set count=%mystrlen%
    call :color_text 2f "++++++++++++++get_suf_sub_str++++++++++++++"
    set substr=
    :intercept_suf_sub_str
    for /f %%i in ("%count%") do (
        if not "!mystr:~%%i,1!"=="!char_sym!" (
            set /a mysubstr_len=!mystrlen! - %%i
            set substr=!mystr:~%%i!
            set /a count-=1
            goto :intercept_suf_sub_str
        )
    )
    echo %0 %mystr% %char_sym% %count% %mysubstr_len%
    call :color_text 9f "--------------get_suf_sub_str--------------"
    endlocal & set %~3=%substr%
goto :eof

:GetCurSysTime
    setlocal EnableDelayedExpansion
    set dateStr=
    set timeStr=
    set year=%date:~6,4%
    set month=%date:~4,2%
    set day=%date:~0,2%
    set dateStr=%date: =_%
    set dateStr=%dateStr:/=x%
    set timeStr=%time::=x%
    set timeStr=%time: =%
    endlocal & set %~1=%dateStr%__%timeStr%
goto :eof
